package com.example.dualscrenn;

import android.app.ActivityOptions;
import android.app.Presentation;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.display.DisplayManager;
import android.media.MediaRouter;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Presentation presentation;
    private Presentation presentation3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1 = (Button)findViewById(R.id.btn_presentation_mediarouter);
        Button btn2 = (Button)findViewById(R.id.btn_presentation_displaymanager);
        Button btn3 = (Button)findViewById(R.id.btn_activity);
//        btn1.setVisibility(View.GONE);
//        btn2.setVisibility(View.GONE);
//        btn3.setVisibility(View.GONE);

        Button third_btn1 = (Button)findViewById(R.id.btn_presentation_mediarouter3);
        Button third_btn2 = (Button)findViewById(R.id.btn_presentation_displaymanager3);
        Button third_btn3 = (Button)findViewById(R.id.btn_activity3);
//        third_btn1.setVisibility(View.GONE);
//        third_btn2.setVisibility(View.GONE);
//        third_btn3.setVisibility(View.GONE);

        showSecondByMediaRouter(MainActivity.this);
        DrawView dv = (DrawView)findViewById(R.id.drawView1);       //获取自定义的绘图视图
        dv.paint.setXfermode(null);                                 //取消擦除效果
        dv.paint.setStrokeWidth(1);                                 //初始化画笔的宽度
        dv.paint.setColor(Color.RED);


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(presentation != null){
                    presentation.cancel();
                }
                showSecondByMediaRouter(MainActivity.this);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(presentation != null){
                    presentation.cancel();
                }
                showSecondByDisplayManager(MainActivity.this);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(presentation != null){
                    presentation.cancel();
                }
                showSecondByActivity(MainActivity.this);
            }
        });


        third_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(presentation3 != null){
                    presentation3.cancel();
                }
                showThirdByMediaRouter(MainActivity.this);
            }
        });
        third_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(presentation3 != null){
                    presentation3.cancel();
                }
                showThirdByDisplayManager(MainActivity.this);
            }
        });
        third_btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(presentation3 != null){
                    presentation3.cancel();
                }
                showSecondByActivity(MainActivity.this);
            }
        });
    }


    private void showSecondByMediaRouter(Context context){
        MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
        if (route != null) {
            Display presentationDisplay = route.getPresentationDisplay();
            if (presentationDisplay != null) {
                presentation = new MyPresentation(context, presentationDisplay);
                presentation.show();
            }
        }
    }

    private void showThirdByMediaRouter(Context context){
        MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
        if (route != null) {
            Display presentationDisplay = route.getPresentationDisplay();
            if (presentationDisplay != null) {
                presentation3 = new MyPresentation3(context, presentationDisplay);
                presentation3.show();
            }
        }
    }

    private void showSecondByDisplayManager(Context context) {
        DisplayManager mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = mDisplayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        if (displays != null) {
            presentation = new MyPresentation(context, displays[displays.length - 1]);
            presentation.show();
        }
    }

    private void showThirdByDisplayManager(Context context) {
        DisplayManager mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = mDisplayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        if (displays != null) {
            presentation3 = new MyPresentation3(context, displays[displays.length - 1]);
            presentation3.show();
        }
    }

    private void showSecondByActivity(Context context){
        ActivityOptions options = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            options = ActivityOptions.makeBasic();
        }
        MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
        if (route != null) {
            Display presentationDisplay = route.getPresentationDisplay();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(presentationDisplay.getDisplayId());
            }
            Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent, options.toBundle());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(presentation != null){
            presentation.cancel();
        }
    }
}
